package com.nbpt.edu.commercesites.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbpt.edu.commercesites.service.GoodService;
import com.nbpt.edu.commercesites.service.StaffService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.*;
import pojo.entity.GoodEntity;
import pojo.vo.GoodVo;
import pojo.vo.GoodVo2;
import pojo.vo.PageResult;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    GoodService goodService;


    @PostMapping("/list")
    Result<List<Good>> getGoodList(){
        return Result.success(goodService.getGoodList());
    }
    @PostMapping("/listmatch")
    Result<List<GoodVo>> getGoodListMatch(){
        return Result.success(goodService.getGoodListMatch());
    }
    @GetMapping("/listmatchbyid")
    Result<GoodVo> getGoodMatchById(Long id){
        return Result.success(goodService.getGoodMatchById(id));
    }
    @PostMapping("/listbyid")
    Result<Good> getGoodById(Long id){
        return Result.success(goodService.getGoodById(id));
    }
    @GetMapping("/listpage")
    PageResult<Good> getGoodListPage(@RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(required = false) String gName,
                                     @RequestParam(required = false) Long gCategoryId,
                                     @RequestParam(required = false) Boolean gHot ){
        return PageResult.success(goodService.getGoodListPage(current,size,gName,gCategoryId,gHot));
    }
    @PostMapping("/insertgood")
    Result<Boolean> insertGood(@RequestBody GoodEntity goodEntity){
        return Result.success(goodService.insertGood(goodEntity));
    }
    @PostMapping("/insertcategory")
    Result<Boolean> insertCategory(@RequestBody Category category){
        return Result.success(goodService.insertCategory(category));
    }
    @PostMapping("/inserttype")
    Result<Boolean> insertType(@RequestBody Type type){
        return Result.success(goodService.insertType(type));
    }
    @GetMapping("/getgoodhot")
    Result<List<Good>> getGoodHot(){
        return Result.success(goodService.getGoodHot());
    }
    @GetMapping("/getgoodnothot")
    Result<List<Good>> getGoodNotHot(){
        return Result.success(goodService.getGoodNotHot());
    }
    @GetMapping("/gettype")
    Result<List<Type>> getType(){
        return Result.success(goodService.getType());
    }
    @GetMapping("/getcategory")
    Result<List<Category>> getCategory(){
        return Result.success(goodService.getCategory());
    }
    @DeleteMapping("/deletebyids/{ids}")
    Result<Boolean> deleteById(@PathVariable String ids){
        String[] idArray=ids.split(",");
        List<Long> idList=new ArrayList<>();
        for (String s:idArray){
            String trimmed = s.trim();
            idList.add(Long.parseLong(trimmed));
        }
        return Result.success(goodService.deleteById(idList));
    }
    @DeleteMapping("/deletecategorybyids/{ids}")
    Result<Boolean> deleteCategoryById(@PathVariable String ids){
        String[] idArray=ids.split(",");
        List<Long> idList=new ArrayList<>();
        for (String i:idArray
             ) {
            String id=i.trim();
            idList.add(Long.parseLong(id));
        }
        return Result.success(goodService.deleteCategoryById(idList));
    }
    @DeleteMapping("/deletetype/{ids}")
    Result<Boolean> deleteTypeById(@PathVariable String ids){
        String[] idArray=ids.split(",");
        List<Long> idList=new ArrayList<>();
        for (String i:idArray
        ) {
            String id=i.trim();
            idList.add(Long.parseLong(id));
        }
        return Result.success(goodService.deleteTypeById(idList));
    }
    @PostMapping("/updategood")
    Result<Boolean> updateGood(@RequestBody GoodVo2 goodVO){
        return Result.success(goodService.updateGood(goodVO));
    }
    @PostMapping("/updatecategory")
    Result<Boolean> updateCategory(@RequestBody Category category){
        return Result.success(goodService.updateCategory(category));
    }
    @GetMapping("/gettypebyid")
    Result<List<String>> getTypeById(Long id){
        return Result.success(goodService.getTypeById(id));
    }
    @GetMapping("/getcategorypage")
    Result<List<Category>> getCategoryPage(@RequestParam(defaultValue = "1") Integer current,
                                           @RequestParam(defaultValue = "10") Integer size){
        return Result.success(goodService.getCategoryPage(current,size));
    }
    @GetMapping("/getgoodbycategory")
    Result<List<Good>> getGoodByCategory(String cName){
        return Result.success(goodService.getGoodByCategory(cName));
    }

}
